﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using MJM.CBD.Conn;
using MJM.SPP.EntidadesNegocio;

namespace MJM.SPP.AccesoDatos
{
    public class ImpresionAD
    {
        public int Insertar(ImpresionEN objImpresionEN)
        {
            SqlConnection conn = new SqlConnection(Cadena.GetCadenaPadron());
            SqlCommand cmd = new SqlCommand("sp_i_T_WEB_IMPTAR_Insertar", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter prmID, prmInicio, prmFin, prmFecha, prmUsuario;
            int codigo = 0;
            try
            {
                prmID = cmd.CreateParameter();
                prmID.Direction = ParameterDirection.ReturnValue;
                prmID.SqlDbType = SqlDbType.Int;
                cmd.Parameters.Add(prmID);

                prmInicio = cmd.CreateParameter();
                prmInicio.ParameterName = "@vTARIMP_inRANINI";
                prmInicio.SqlDbType = SqlDbType.Int;
                prmInicio.Value = objImpresionEN.TARIMP_inRANINI;
                cmd.Parameters.Add(prmInicio);

                prmFin = cmd.CreateParameter();
                prmFin.ParameterName = "@vTARIMP_inRANFIN";
                prmFin.SqlDbType = SqlDbType.Int;
                prmFin.Value = objImpresionEN.TARIMP_inRANFIN;
                cmd.Parameters.Add(prmFin);

                prmFecha = cmd.CreateParameter();
                prmFecha.ParameterName = "@vTARIMP_chFECREG";
                prmFecha.SqlDbType = SqlDbType.VarChar;
                prmFecha.Value = objImpresionEN.TARIMP_chFECREG;
                cmd.Parameters.Add(prmFecha);

                prmUsuario = cmd.CreateParameter();
                prmUsuario.ParameterName = "@vTARIMP_chUSUREG";
                prmUsuario.SqlDbType = SqlDbType.VarChar;
                prmUsuario.Value = objImpresionEN.TARIMP_chUSUREG;
                cmd.Parameters.Add(prmUsuario);

                cmd.Connection.Open();
                cmd.ExecuteNonQuery();
                codigo = Convert.ToInt32(prmID.Value);
                cmd.Connection.Close();
                cmd.Dispose();
                conn.Dispose();
            }
            catch(Exception ex)
            {
                codigo = 0;
                cmd.Dispose();
                conn.Dispose();
            }
            return codigo;
        }

        public int Modificar(int ID)
        {
            SqlConnection conn = new SqlConnection(Cadena.GetCadenaPadron());
            SqlCommand cmd = new SqlCommand("sp_u_T_WEB_IMPTAR_Modificar", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter prmID;
            int codigo = 1;
            try
            {
                prmID = cmd.CreateParameter();
                prmID.ParameterName = "@vTARIMP_P_inID";
                prmID.SqlDbType = SqlDbType.Int;
                prmID.Value = ID;
                cmd.Parameters.Add(prmID);
                cmd.Connection.Open();
                cmd.ExecuteNonQuery();
                cmd.Connection.Close();
                cmd.Dispose();
                conn.Dispose();
            }
            catch(Exception ex)
            {
                codigo = 0; 
                cmd.Dispose();
                conn.Dispose();
            }
            return codigo;
        }

        public ImpresionEN Obtener(int ID)
        {
            SqlConnection conn = new SqlConnection(Cadena.GetCadenaPadron());
            SqlCommand cmd = new SqlCommand("sp_s_T_WEB_IMPTAR_Obtener", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter prmID;
           ImpresionEN obj=new ImpresionEN();
           SqlDataReader dr;
            try
            {
                prmID = cmd.CreateParameter();
                prmID.ParameterName = "@vTARIMP_P_inID";
                prmID.SqlDbType = SqlDbType.Int;
                prmID.Value = ID;

                cmd.Parameters.Add(prmID);
                cmd.Connection.Open();
                dr=cmd.ExecuteReader();
                if (dr.Read())
                {
                    obj.TARIMP_P_inID = dr.GetInt32(0);
                    obj.TARIMP_inRANINI = dr.GetInt32(1);
                    obj.TARIMP_inRANFIN = dr.GetInt32(2);
                    obj.TARIMP_chFECREG = dr.GetString(3);
                    obj.TARIMP_chUSUREG = dr.GetString(4);
                    obj.TARIMP_inFLAELI = dr.GetInt32(5);
                }

                cmd.Connection.Close();
                cmd.Dispose();
                conn.Dispose();
            }
            catch (Exception ex)
            {                
                cmd.Dispose();
                conn.Dispose();
            }
            return obj;
        }
    }
}
